package com.neudu.ssrs_yyzxservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.pojo.Beddetails;
import com.neudu.ssrs_yyzxservice.pojo.Customer;
import com.neudu.ssrs_yyzxservice.service.BeddetailsService;
import com.neudu.ssrs_yyzxservice.service.CustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BeddetailsService beddetailsService;
    @Autowired
    private JSONReturn jsonReturn;

    // 新增获取客户列表的方法
    @GetMapping("/get_Customer_List")
    public String getCustomerList() {
        try {
            QueryWrapper<Customer> wrapper = new QueryWrapper<>();
            List<Customer> customerList = customerService.list(wrapper);
            return jsonReturn.returnSuccess(customerList);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    @RequestMapping("get_all_customers")
    public String getAll(@RequestBody PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            //可以根据数据库设置一个state字段值，匹配在住的客户查询
            List<Customer> list = customerService.list();
            PageInfo<Customer> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_customers_by")
    public String getByCondition(@RequestBody Customer customer){
        try {
            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(customer.getId()), Customer::getId, customer.getId())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerAge()), Customer::getCustomerAge, customer.getCustomerAge())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerSex()), Customer::getCustomerSex, customer.getCustomerSex())
                    .like(!ObjectUtils.isEmpty(customer.getCustomerName()), Customer::getCustomerName, customer.getCustomerName())
                    .eq(!ObjectUtils.isEmpty(customer.getBuildingNo()), Customer::getBuildingNo, customer.getBuildingNo());
            List<Customer> customers = customerService.list(queryWrapper);
            return jsonReturn.returnSuccess(customers);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("add_one_customer")
    public String addOne(@RequestBody Customer customer)  {
        try {
            // 保存 Customer 对象
            customerService.save(customer);

            Beddetails beddetails = new Beddetails();
            // 复制匹配的属性
            BeanUtils.copyProperties(customer, beddetails);

            // 手动处理不匹配的属性
            // 假设床位起始日期和结束日期可以用入住时间和合同到期时间来代替
            beddetails.setStartDate(customer.getCheckinDate());
            beddetails.setEndDate(customer.getExpirationDate());
            // 假设床位详情信息可以根据业务需求进行设置
            beddetails.setBedDetails("-");
            // 设置顾客 ID
            beddetails.setCustomerId(customer.getId());

            // 保存 Beddetails 对象
            beddetailsService.save(beddetails);

            return jsonReturn.returnSuccess();
        }catch (Exception e){
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }

    }

    @RequestMapping("getcustomerby_id")
    public String getById(@RequestParam("customerid") Integer customerid){
        try {
            Customer customer = customerService.getById(customerid);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifycustomerby_id")
    public String modifyById(@RequestBody Customer customer){
        try {
            customerService.updateById(customer);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removecustomer_by_id")
    public String removeByid(@RequestParam("customerid") Integer customerid){
        try {
            customerService.removeById(customerid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}
